<template>
  <section class="primarygradesortstyle">
    <!-- 标题 -->
    <h2>{{ datas.text }}</h2>

    <!-- 表单 -->
    <el-form label-width="100px" :model="datas" size="small" :rules="rules">
      <!-- 选择样式 -->
      <el-form-item label="选择样式" class="lef">
        <p style="color: #000">{{ styleText }}</p>
      </el-form-item>

      <!-- 样式选择 -->
      <div class="commodityType">
        <el-tooltip class="item" effect="dark" content="样式一" placement="bottom">
          <span
            class="iconfont icon-Component-biaotiwenzi"
            style="font-size: 21px"
            :class="datas.commodityType === 0 ? 'active' : ''"
            @click="datas.commodityType = 0"
          />
        </el-tooltip>

        <el-tooltip class="item" effect="dark" content="样式二" placement="bottom">
          <span
            class="iconfont icon-fuwenben"
            style="font-size: 24px"
            :class="datas.commodityType === 1 ? 'active' : ''"
            @click="datas.commodityType = 1"
          />
        </el-tooltip>
      </div>

      <!-- 下划线 -->
      <div class="bor" />
      <div style="height: 20px" />

      <!-- 左侧标题图片 -->
      <el-form-item label="左侧标题图片">
        <div class="backgroundImg">
          <img v-if="!datas.titlePic" draggable="false" src="../../../assets/images/add.png" alt="" @click="uploadI('titlePic')" />
          <div v-else>
            <img draggable="false" :src="datas.titlePic" alt="" />
            <p @click="deleteupimg('titlePic')">删除</p>
          </div>
        </div>
      </el-form-item>

      <!-- 左侧标题 -->
      <el-form-item label="左侧标题">
        <el-input v-model="datas.name" placeholder="请输入标题" show-word-limit clearable/>
      </el-form-item>

      <div style="height: 10px" />

      <!-- 左侧标题描述 -->
      <el-form-item label="左侧标题描述">
        <el-input
          v-model="datas.description"
          type="textarea"
          placeholder="请输入要说明的文字，最多 100 字"
          maxlength="100"
          @focus="descInputFocus"
        />
      </el-form-item>

      <div style="height: 10px" />

      <!-- 学习指导 -->
      <el-form-item label="学习指导" class="lef">
        {{ datas.isShowRight ? "显示" : "隐藏" }}
        <el-checkbox v-model="datas.isShowRight" style="margin-left: 196px" />
      </el-form-item>
      <div v-show="datas.isShowRight">
       <!-- 右侧标题 -->
        <el-form-item label="右侧标题">
          <el-input v-model="datas.rightText" placeholder="请输入标题" show-word-limit clearable/>
        </el-form-item>
        <!-- 右侧标题图片 -->
        <el-form-item label="右侧标题图片">
          <div class="backgroundImg">
            <img v-if="!datas.rightPic" draggable="false" src="../../../assets/images/add.png" alt="" @click="uploadI('rightPic')" />
            <div v-else>
              <img draggable="false" :src="datas.rightPic" alt="" />
              <p @click="deleteupimg('rightPic')">删除</p>
            </div>
          </div>
        </el-form-item>
        <!-- 右侧视频 -->
        <el-form-item label="右侧视频">
          <div class="backgroundImg">
            <el-button v-if="!datas.rightVideoUrl" size="small" type="primary" @click="uploadVideo">
              点击上传视频<i class="el-icon-upload el-icon--right"></i>
            </el-button>
            <div v-else>
              <video width="100%" v-if="datas.rightVideoUrl" controls="controls">
                <source :src="datas.rightVideoUrl" type="video/mp4" />
              </video>
              <p @click="deleteupvideo">删除</p>
            </div>
          </div>
        </el-form-item>
        <!-- 右侧链接 -->
        <el-form-item label="右侧链接">
          <select-link @selectThisLink="selectThisLink" @clearThisLink="clearThisLink" :linktypeurl="datas.rightApiUrl" />
        </el-form-item>
      </div>

      <!-- 年级选择 -->
      <el-form-item class="lef" label="年级选择">
        <el-cascader :options="gradelist" v-model="datas.activeGrade" :props="{ multiple: true }" @change="chooseGrade" clearable></el-cascader>
      </el-form-item>

      <!-- 显示位置 -->
      <el-form-item label="显示位置">
        <div class="weiz">
          <i
            :class="datas.positions === 'left' ? 'active' : ''"
            class="iconfont icon-horizontal-left"
            @click="datas.positions = 'left'"
          />
          <i
            :class="datas.positions === 'center' ? 'active' : ''"
            class="iconfont icon-juzhong"
            @click="datas.positions = 'center'"
          />
        </div>
      </el-form-item>

      <div style="height: 10px" />

      <!-- 标题大小 -->
      <el-form-item label="标题大小" prop="nameSize" :hide-required-asterisk="true">
        <el-input v-model.number="datas.nameSize" type="number" placeholder="请输入标题大小" maxlength="2" />
      </el-form-item>

      <div style="height: 10px" />

      <!-- 描述大小 -->
      <el-form-item label="描述大小" prop="descriptionSize" :hide-required-asterisk="true">
        <el-input v-model.number="datas.descriptionSize" type="number" placeholder="请输入描述大小" maxlength="2" />
      </el-form-item>

      <div style="height: 10px" />

      <!-- 标题粗细 -->
      <el-form-item label="标题粗细" prop="nameWeight" :hide-required-asterisk="true">
        <el-input v-model.number="datas.nameWeight" type="number" placeholder="请输入标题粗细" maxlength="3" />
      </el-form-item>

      <div style="height: 10px" />

      <!--描述粗细 -->
      <el-form-item  label="描述粗细" prop="descriptionWeight" :hide-required-asterisk="true">
        <el-input v-model.number="datas.descriptionWeight" type="number" placeholder="请输入描述粗细" maxlength="3" />
      </el-form-item>

      <div style="height: 10px" />

      <!-- 标题颜色 -->
      <el-form-item label="标题颜色">
        <!-- 颜色选择器 -->
        <el-color-picker v-model="datas.nameColor" show-alpha  class="picke" :predefine="predefineColors"/>
      </el-form-item>

      <div style="height: 10px" />

      <!-- 描述颜色 -->
      <el-form-item label="描述颜色">
        <!-- 颜色选择器 -->
        <el-color-picker v-model="datas.descriptionColor" show-alpha  class="picke" :predefine="predefineColors"/>
      </el-form-item>
    </el-form>
    <!-- 上传图片 -->
    <uploadimg ref="upload" @uploadInformation="uploadInformation" />
    <!-- 上传视频 -->
    <uploadVideo ref="uploadvideo" @uploadVideoSrc="uploadVideoSrc" />
  </section>
</template>

<script>
import uploadimg from "../../uploadImg"; // 图片上传
import uploadVideo from "../../uploadVideo"; // 视频上传
import SelectLink from "../../selectLink";
export default {
  name: "Primarygradesortstyle",
  props: {
    datas: Object,
  },
  components: {
    uploadimg,
    uploadVideo,
    SelectLink
  },
  computed: {
    styleText() {
      let data;
      if (this.datas.commodityType === 0) data = "样式一";
      if (this.datas.commodityType === 1) data = "样式二";
      return data;
    },
  },
  data() {
    const checkNameSize = (rule, value, callback) => {
      if (value.length === 0) callback(new Error("请输入有效数字"));
      if (value > 60) {
        callback(new Error("数字不能超过60"));
        this.datas.nameSize = '16';
      } else {
        this.datas.nameSize = value;
      }
    };
    const checkDescriptionSize = (rule, value, callback) => {
      if (value.length === 0) callback(new Error("请输入有效数字"));
      if (value > 60) {
        callback(new Error("数字不能超过60"));
        this.datas.descriptionSize = '12';
      } else {
        this.datas.descriptionSize = value;
      }
    };
    const checkNameWeight = (rule, value, callback) => {
      if (value.length === 0) callback(new Error("请输入有效数字"));
      if (value > 900) {
        callback(new Error("数字不能超过900"));
        this.datas.nameWeight = '600';
      } else {
        this.datas.nameWeight = value;
      }
    };
    const checkDescriptionWeight = (rule, value, callback) => {
      if (value.length === 0) callback(new Error("请输入有效数字"));
      if (value > 900) {
        callback(new Error("数字不能超过900"));
        this.datas.descriptionWeight = '200';
      } else {
        this.datas.descriptionWeight = value;
      }
    };
    return {
      options: [],
      rules: {
        nameSize: [{ required: true, validator: checkNameSize, trigger: "blur" }],
        descriptionSize: [{ required: true, validator: checkDescriptionSize, trigger: "blur" }],
        nameWeight: [{ required: true, validator: checkNameWeight, trigger: "blur" }],
        descriptionWeight: [{ required: true, validator: checkDescriptionWeight, trigger: "blur" }],
      },
      predefineColors: [
        // 颜色选择器预设
        "#ff4500",
        "#ff8c00",
        "#ffd700",
        "#90ee90",
        "#00ced1",
        "#1e90ff",
        "#c71585",
        "#409EFF",
        "#909399",
        "#C0C4CC",
        "rgba(255, 69, 0, 0.68)",
        "rgb(255, 120, 0)",
        "hsv(51, 100, 98)",
        "hsva(120, 40, 94, 0.5)",
        "hsl(181, 100%, 37%)",
        "hsla(209, 100%, 56%, 0.73)",
        "#c7158577",
      ],
      imgtype: '',
      gradelist: [
        {
          value: "0001",
          label: "小学",
          children: [
            { 
              value: "0001", 
              label: "一年级"
            },
            { 
              value: "0002", 
              label: "二年级"
            },
            { 
              value: "0003", 
              label: "三年级"
            },
            { 
              value: "0004", 
              label: "四年级"
            },
            { 
              value: "0005", 
              label: "五年级"
            },
            { 
              value: "0006", 
              label: "六年级"
            }, 
          ],
        },
        {
          value: "0002",
          label: "初中",
          children: [
            { 
              value: "0007", 
              label: "七年级"
            },
            { 
              value: "0008", 
              label: "八年级"
            },
            { 
              value: "0009", 
              label: "九年级"
            },
          ],
        },
        {
          value: "0003",
          label: "高中",
          children: [
            { 
              value: "0011", 
              label: "高中",
            }
          ],
        },
      ],
    };
  },
  methods: {
    inputFocus() {
      this.datas.name = "";
    },
    descInputFocus() {
      this.datas.description = "";
    },
    chooseGrade() {
      const valObj = this.datas.activeGrade.map(item => ({
        stageCode: item[0],
        gradeCode: item[1]
      }));
      this.datas.classList = valObj;
    },
    /* 上传图片 */
    uploadI(type) {
      this.imgtype = type;
      this.$refs.upload.showUpload();
    },
    uploadVideo() {
      this.$refs.uploadvideo.showUpload();
    },
    // 删除标题图片
    deleteupimg(type) {
      if (type === 'titlePic') {
        this.datas.titlePic = "";
      } else if(type === 'rightPic') {
        this.datas.rightPic = "";
      }
    },
    // 删除视频
    deleteupvideo() {
      this.datas.rightVideoUrl = "";
    },
    uploadVideoSrc(url) {
      this.datas.rightVideoUrl = url;
    },
    // 提交
    uploadInformation(res) {
      if (this.imgtype === 'titlePic') {
        this.datas.titlePic = res;
      } else if(this.imgtype === 'rightPic') {
        this.datas.rightPic = res;
      }
    },
    selectThisLink(resu) {
      this.datas.rightApiUrl = resu.url;
    },
    clearThisLink () {
      this.datas.rightApiUrl = '';
    }
  },
};
</script>

<style scoped lang="scss">
.primarygradesortstyle {
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  padding: 0 10px 20px;
  box-sizing: border-box;

  .backgroundImg {
    cursor: pointer;
    position: relative;
    width: 60px;
    height: 60px;
    img {
      width: 100%;
      height: auto;
    }
    p {
      position: absolute;
      left: 0;
      bottom: -10px;
      width: 100%;
      height: 20px;
      font-size: 12px;
      color: #fff;
      background: rgba(0, 0, 0, 0.5);
      text-align: center;
      line-height: 20px;
    }
  }

  .lef {
    .el-form-item__label {
      text-align: left;
    }
    p {
      line-height: 4px;
    }
  }

  /* 样式 */
  .commodityType {
    display: flex;
    justify-content: space-around;
    align-items: center;
    span {
      display: inline-block;
      width: 58px;
      height: 32px;
      text-align: center;
      line-height: 32px;
      background: #ebedf0;
      color: #979797;
      border: 1px solid #fff;
      cursor: pointer;
      transition: all 0.5s;

      &:hover {
        border: 1px solid #155bd4;
        color: #155bd4;
      }

      &.active {
        border: 1px solid #155bd4;
        background-color: #e0edff;
        color: #155bd4;
      }
    }
  }

  /* 标题 */
  h2 {
    padding: 24px 16px 24px 0;
    margin-bottom: 15px;
    border-bottom: 1px solid #f2f4f6;
    font-size: 18px;
    font-weight: 600;
    color: #323233;
  }

  /* 颜色选择器 */
  .picke {
    float: right;
  }

  /* 位置 */
  .weiz {
    text-align: right;
    i {
      padding: 5px 14px;
      margin-left: 10px;
      border-radius: 0;
      border: 1px solid #ebedf0;
      font-size: 20px;
      font-weight: 500;
      cursor: pointer;

      &:last-child {
        font-size: 22px;
      }

      &.active {
        color: #155bd4;
        border: 1px solid #155bd4;
        background: #e0edff;
      }
    }
  }

  .wid .el-form-item__label {
    width: 94px !important;
  }
  .wid .el-form-item__content {
    float: right;
  }
}
</style>
